<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="广告名称" prop="ggName">
      <el-input v-model="dataForm.ggName" placeholder="广告名称"></el-input>
    </el-form-item>
    <el-form-item label="广告类型" prop="ggType">
      <el-input v-model="dataForm.ggType" placeholder="广告类型"></el-input>
    </el-form-item>
    <el-form-item label="广告图片链接" prop="ggUrl">
      <single-upload v-model="dataForm.ggUrl"></single-upload>    
    </el-form-item>
    <el-form-item label="广告生效日期" prop="ggEtime">
      <el-input v-model="dataForm.ggEtime" placeholder="广告生效日期"></el-input>
    </el-form-item>
    <el-form-item label="广告失效日期" prop="ggFtime">
      <el-input v-model="dataForm.ggFtime" placeholder="广告失效日期"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder="逻辑删除"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from "./singleUpload.vue"
  export default {
    components:{SingleUpload},
    data () {
      return {
        visible: false,
        dataForm: {
          ggId: 0,
          ggName: '',
          ggType: '',
          ggUrl: '',
          ggEtime: '',
          ggFtime: '',
          isDelete: ''
        },
        dataRule: {
          ggName: [
            { required: true, message: '广告名称不能为空', trigger: 'blur' }
          ],
          ggType: [
            { required: true, message: '广告类型不能为空', trigger: 'blur' }
          ],
          ggUrl: [
            { required: true, message: '广告链接不能为空', trigger: 'blur' }
          ],
          ggEtime: [
            { required: true, message: '广告生效日期不能为空', trigger: 'blur' }
          ],
          ggFtime: [
            { required: true, message: '广告失效日期不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '逻辑删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.ggId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.ggId) {
            this.$http({
              url: this.$http.adornUrl(`/admin/ssmadvertising/info/${this.dataForm.ggId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.ggName = data.ssmAdvertising.ggName
                this.dataForm.ggType = data.ssmAdvertising.ggType
                this.dataForm.ggUrl = data.ssmAdvertising.ggUrl
                this.dataForm.ggEtime = data.ssmAdvertising.ggEtime
                this.dataForm.ggFtime = data.ssmAdvertising.ggFtime
                this.dataForm.isDelete = data.ssmAdvertising.isDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/admin/ssmadvertising/${!this.dataForm.ggId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'ggId': this.dataForm.ggId || undefined,
                'ggName': this.dataForm.ggName,
                'ggType': this.dataForm.ggType,
                'ggUrl': this.dataForm.ggUrl,
                'ggEtime': this.dataForm.ggEtime,
                'ggFtime': this.dataForm.ggFtime,
                'isDelete': this.dataForm.isDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
